$(document).ready(function() {

    /*
    var url = '/demo/server/php/';
    $('#fileupload').fileupload({
        formData: {thumb_width: '40'},
        url: url,
        dataType: 'json',
        done: function (e, data) {
            $.each(data.result.files, function (index, file) {
                $('<p/>').text(file.name).appendTo('#files');
            });
        },
        progressall: function (e, data) {
            var progress = parseInt(data.loaded / data.total * 100, 10);
            $('#progress .progress-bar').css(
                'width',
                progress + '%'
            );
            //if(progress == 100)
                //alert('DONE');
        }
    }).prop('disabled', !$.support.fileInput)
        .parent().addClass($.support.fileInput ? undefined : 'disabled');
    */
    
    $('.btn_add_image img').click(function(){
        var len_other_image = $('.other_image > div').length;
        var html = '<div class="btn-group btn-group-xs btn-group-solid"><img id="other_image_'+len_other_image+'" class="show_image_browse" src="'+webroot+'upload/images/no-image.jpg" /><input class="order_other_image" type="text" value="" name="order_image[]" title="Sort order"/><input class="other_image_'+len_other_image+'" type="hidden" name="other_image[]" value="'+webroot+'upload/images/no-image.jpg" /><div class="clear"></div><button class="btn blue fileBrowse" data-target="other_image_'+len_other_image+'" type="button">Browse File</button><button class="btn red fileDelete" data-target="other_image_'+len_other_image+'" type="button">Delete File</button></div>';
        $('.other_image').prepend(html);
    });
    
    
    $(".fileBrowse").live('click',function(){
            var finder = new CKFinder();
            var target = $(this).attr('data-target');
            finder.selectActionFunction = function (fileUrl, data) {
                $('#'+target).attr('src',fileUrl);
                $('.'+target).val(fileUrl);
            }
            finder.popup();
        });
        
    $(".fileDelete").live('click',function(){
        var target = $(this).attr('data-target');
            $('#'+target).attr('src', webroot+'upload/images/no-image.jpg');
            $('.'+target).val(webroot+'upload/images/no-image.jpg');
    });
    
});

function open_modal_basic(title, content, link)
{
    var obj_modal = $("#modal_basic");
    obj_modal.find('.modal-title').html(title);
    obj_modal.find('.modal-body').html(content);
    if(link != 'undefined')
    {
        obj_modal.find('button.btn-modal-close').attr('onclick','window.location=\''+link+'\'');
    }
    obj_modal.modal('show');
}

function open_modal_confirm(title, content, array_attr)
{
    console.log(array_attr);
    var text_attr = "";
    
    console.log(text_attr);
    
    var obj_modal = $("#modal_confirm");
    obj_modal.find('.modal-title').html(title);
    obj_modal.find('.modal-body').html(content);
    if(array_attr != 'undefined')
    {
        var key;
        var value;
        for (key in array_attr) {
            value = array_attr[key];
            obj_modal.find('button.btn-modal-accept').attr(key, value);
        }
    }
    obj_modal.modal('show');
}